package com.lin.carbon.service.impl;

import com.lin.carbon.dto.RegisterRequest;
import com.lin.carbon.entity.SysRegisterAccount;
import com.lin.carbon.mapper.SysAccountInsertMapper;
import com.lin.carbon.service.SysAccountRegisterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysAccountRegisterServiceImpl implements SysAccountRegisterService {

    private static final Logger logger = LoggerFactory.getLogger(SysAccountRegisterServiceImpl.class);

    @Autowired
    private SysAccountInsertMapper sysAccountInsertMapper;

    @Override
    @Transactional
    public boolean register(RegisterRequest request) {
        SysRegisterAccount sysRegisterAccount = new SysRegisterAccount();
        sysRegisterAccount.setUsername(request.getUsername());
        sysRegisterAccount.setPassword(request.getPassword());
        sysRegisterAccount.setEmail(request.getEmail());
        sysRegisterAccount.setPhone(request.getPhone());
        sysRegisterAccount.setAccountName(request.getAccountName());

        int result = sysAccountInsertMapper.insertAccount(sysRegisterAccount);
        logger.info("插入操作返回值（受影响的行数）: {}", result);
        return result > 0;

    }

    @Override
    public boolean check(RegisterRequest request) {
        SysRegisterAccount sysRegisterAccount = new SysRegisterAccount();
        sysRegisterAccount.setUsername(request.getUsername());
        return sysAccountInsertMapper.selectAccountByUsername(sysRegisterAccount.getUsername()) > 0;
    }
}